home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / muds / pennmush.000 / pennmush-1.50-p8-linux.tar / pennmush / utils.c < prev    next >
C/C++ Source or Header  |  1993-04-14  |  4KB  |  243 lines

  1. /* utils.c */
  2.  
  3. #include "copyright.h"
  4.  
  5. #include <stdio.h>
  6.  
  7. #include "config.h"
  8. #include "interface.h"
  9. #include "db.h"
  10.  
  11. #ifdef HPUX
  12. #include <stdlib.h>
  13. #endif
  14.  
  15. void parse_attrib(player, str, thing, attrib)
  16.      dbref player;
  17.      char *str;
  18.      dbref *thing;
  19.      ATTR **attrib;
  20. {
  21.   /* takes a string which is of the format <obj>/<attr> or <attr>,
  22.    * and returns the dbref of the object, and a pointer to the attribute.
  23.    * If no object is specified, then the dbref returned is the player's.
  24.    * str is destructively modified.
  25.    */
  26.  
  27.   char *name;
  28.  
  29.   /* find the object */
  30.  
  31.   if ((name = (char *) index(str, '/')) != NULL) {
  32.     *name++ = '\0';
  33.     init_match(player, str, NOTYPE);
  34.     match_everything();
  35.     *thing = noisy_match_result();
  36.   } else {
  37.     name = str;
  38.     *thing = player;
  39.   }
  40.  
  41.   /* find the attribute */
  42.   *attrib = (ATTR *) atr_get(*thing, upcasestr(name));
  43. }
  44.  
  45.  
  46. dbref find_entrance(door)
  47.     dbref door;
  48. {
  49.   dbref room;
  50.   dbref thing;
  51.   for (room = 0; room < db_top; room++)
  52.     if (Typeof(room) == TYPE_ROOM) {
  53.       thing = db[room].exits;
  54.       while (thing != NOTHING) {
  55.     if (thing == door)
  56.       return room;
  57.     thing = db[thing].next;
  58.       }
  59.     }
  60.   return NOTHING;
  61. }
  62.  
  63. /* remove the first occurence of what in list headed by first */
  64. dbref remove_first(first, what)
  65.     dbref first;
  66.     dbref what;
  67. {
  68.   dbref prev;
  69.   /* special case if it's the first one */
  70.   if (first == what) {
  71.     return db[first].next;
  72.   } else {
  73.     /* have to find it */
  74.     DOLIST(prev, first) {
  75.       if (db[prev].next == what) {
  76.     db[prev].next = db[what].next;
  77.     return first;
  78.       }
  79.     }
  80.     return first;
  81.   }
  82. }
  83.  
  84. int member(thing, list)
  85.     dbref thing;
  86.     dbref list;
  87. {
  88.   DOLIST(list, list) {
  89.     if (list == thing)
  90.       return 1;
  91.   }
  92.  
  93.   return 0;
  94. }
  95.  
  96. int recursive_member(disallow, from, count) 
  97.     dbref disallow;
  98.     dbref from;
  99.     int count;
  100. {
  101.   dbref contents = db[from].contents;
  102.  
  103.   if (count > 50) return 0;
  104.   DOLIST(contents, contents) {
  105.     count++;
  106.     return ((from == disallow) ||
  107.         (contents == disallow) ||
  108.         recursive_member(disallow, contents, count));
  109.   }
  110.   return 0;
  111. }
  112.  
  113. dbref reverse(list)
  114.     dbref list;
  115. {
  116.   dbref newlist;
  117.   dbref rest;
  118.   newlist = NOTHING;
  119.   while (list != NOTHING) {
  120.     rest = db[list].next;
  121.     PUSH(list, newlist);
  122.     list = rest;
  123.   }
  124.   return newlist;
  125. }
  126.  
  127. /* takes a dbref and returns a pointer to the head of a dblist */
  128. struct dblist *
  129. listcreate(ref)
  130. dbref ref;
  131. {
  132.   struct dblist *ptr;
  133.   
  134.   ptr = (struct dblist *)malloc((unsigned)sizeof(struct dblist));
  135.   if (!ptr)
  136.       panic("Out of memory.");
  137.   ptr->obj = ref;
  138.   ptr->next = NULL;
  139.   return(ptr);
  140. }
  141.  
  142. /*
  143.  * takes a pointer to a dblist and a dbref and adds the dbref to the
  144.  * end of the list.
  145.  */
  146. void
  147. listadd(head, ref)
  148. struct dblist *head;
  149. dbref ref;
  150. {
  151.   struct dblist *ptr = head;
  152.     
  153.  
  154.   while (ptr->next)
  155.       ptr = ptr->next;
  156.   
  157.   ptr->next = listcreate(ref);
  158. }
  159.  
  160. /* takes a pointer to a dblist and recursively frees it */
  161. void
  162. listfree(head)
  163. struct dblist *head;
  164. {
  165.   struct dblist *ptra = head, *ptrb;
  166.  
  167.   while (ptra->next) {
  168.     ptrb = ptra->next;
  169.     free(ptra);
  170.     ptra = ptrb;
  171.   }
  172. }
  173.  
  174. int my_random(x)
  175.      int x;
  176. {
  177.   int result;
  178. #ifdef HPUX
  179.   result = (int) ((float)x * ((float)rand() / RAND_MAX));
  180. #else
  181.    result = (int) ( ((float)x * (float)rand())/2147477000.0);
  182. #endif
  183.   return result;
  184. }
  185.  
  186. int getrandom(x)
  187.      int x;
  188. {
  189.    int temprandoms[20];
  190.    int i;
  191.    if (x < 1)
  192.       x = 1;
  193.    for (i = 0; i < 20; i++) 
  194.       temprandoms[i] = my_random(x);
  195.    i = my_random(20);
  196.  
  197.    return temprandoms[i];
  198. }
  199.  
  200. unsigned hash_fn(s, hashtab_mask)
  201.      char *s;
  202.      int hashtab_mask;
  203. {
  204.   /* hash function, using masks (based on TinyMUSH 2.0) */
  205.  
  206.   unsigned hashval;
  207.   char *p;
  208.  
  209.   for (hashval = 0, p = s; *p; p++)
  210.     hashval = (hashval << 5) + hashval + *p;
  211.   return (hashval & hashtab_mask);
  212. }
  213.  
  214. int is_number(str)
  215.      char *str;
  216. {
  217.     /* is a string a number? */
  218.  
  219.     while (*str && isspace(*str))    /* trim leading spaces */
  220.     str++;
  221.  
  222.     if (*str == '-') {
  223.     str++;
  224.     if (!*str)
  225.         return 0;            /* just a minus sign. no good. */
  226.     }
  227.  
  228.     while (*str && isdigit(*str))    /* the number */
  229.     str++;
  230.  
  231. #ifdef FLOATING_POINTS
  232.     if (*str == '.')        /* decimal point */
  233.     str++;
  234.     while (*str && isdigit(*str))    /* the fractional part */
  235.     str++;
  236. #endif
  237.  
  238.   while (*str && isspace(*str))    /* trim trailing spaces */
  239.     str++;
  240.  
  241.   return (*str ? 0 : 1);
  242. }
  243.